package com.cloud.shopping.druid.util;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

/**
 * @author John.zhang
 * @version Id: DruidPage, v 1.0 2021/2/8 9:46 ZhiYong Exp $
 */

@Data
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class DruidPage<T> {

    /**
     * 总记录数
     */
    private Integer total;
    /**
     * 每页显示条数
     */
    private Integer size;
    /**
     * 当前页
     */
    private Integer page;
    /**
     * 偏移量
     */
    private Long offset;
    /**
     * 总页数
     */
    private Integer totalPages;
    /**
     * 数据集合
     */
    private T data;
    /**
     * 排序字段
     */
    private String sort;
    /**
     * 排序类型
     */
    private String sortType;


    /***
     * 初始化并计算偏移量
     * @param sort sort
     * @param size size
     * @param sortType sortType
     * @param page page
     */
    public DruidPage(Integer page,Integer size,String sort,String sortType) {
        this.size = size;
        this.page = page;
        this.sort = sort;
        this.sortType = sortType;

        if(page<=0){
            this.page = 1;
        }
        //计算偏移量
        this.offset = (long) ((this.page - 1) * size);
    }

    /***
     * 初始化并计算偏移量
     * @param size size
     * @param page page
     */
    public DruidPage(Integer page,Integer size) {
        this.size = size;
        this.page = page;

        if(page<=0){
            this.page = 1;
        }
        //计算偏移量
        this.offset = (long) ((this.page - 1) * size);
    }

    /**
     * 计算分页参数
     * @param data data
     * @param total total
     * @return DruidPage
     */
    public DruidPage<T> pages(T data,Integer total){
        //总记录数
        this.total = total;
        //数据
        this.data = data;
        //总页数
        if(this.total>0){
            this.totalPages = this.total%this.size==0? this.total/this.size : (this.total/this.size)+1;
        }else{
            this.totalPages = 0;
        }
        return this;
    }
}
